package com.a918.controller;

import com.a918.dto.BwgData;
import com.a918.dto.TableResponse;
import com.a918.entity.FrontFcCheck;
import com.a918.entity.FrontMember;
import com.a918.entity.FrontUnion;
import com.a918.entity.MemBwgWithBLOBs;
import com.a918.service.BackContentService;
import com.a918.service.BackShowService;
import com.a918.util.FileUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
@Controller
public class BackShowController {
    private BackShowService backShowService;
    private BackContentService backContentService;

    @Autowired
    public BackShowController(BackShowService backShowService, BackContentService backContentService){
        this.backShowService=backShowService;
        this.backContentService=backContentService;
    }

    @GetMapping("/backshow")
    public String backshow(HttpServletRequest req,Model model){
        String type = req.getParameter("type");
        Integer userid=(Integer) req.getSession().getAttribute("id");
        switch (type){
            case "hyfc":
                FrontUnion frontUnion =new FrontUnion();
                if (backShowService.getFrontUnionByUserid(userid)!=null){
                    frontUnion=backShowService.getFrontUnionByUserid(userid);
                }
                model.addAttribute(frontUnion);
                return "back/show_memberaddu";
            case "bwgzt":
                List<MemBwgWithBLOBs> bwgList =backShowService.getAvalMuseByUserid(userid);
                model.addAttribute("bwgList",bwgList);
                return "back/show_bwgzt";
        }
        return null;

    }

    @GetMapping("/bwgztEdit")
    public String bwgztEdit(HttpServletRequest req,Model model){
        Integer id=Integer.parseInt(req.getParameter("id"));
        FrontMember frontMember =backShowService.getFrontMemberById(id);
        if (frontMember.getPicid()==null){
            frontMember.setPicid("/static/images/bwglogo/0.png");
        }
        model.addAttribute(frontMember);
        return "back/show_bwgztEdit";
    }

    @GetMapping("/go_bwgztEdit")
    public String go_bwgztEdit(HttpServletRequest req,Model model){
        Integer bwgid=Integer.parseInt(req.getParameter("bwgid"));
        FrontMember frontMember=new FrontMember();
        MemBwgWithBLOBs memBwgWithBLOBs = backShowService.getBwgById(bwgid);
        String name = memBwgWithBLOBs.getName();
        frontMember.setBwgid(bwgid);
        frontMember.setName(name);
        frontMember.setPicid("/static/images/bwglogo/0.png");
        model.addAttribute(frontMember);
        return "back/show_bwgztEdit";
    }

    @PostMapping("/bwgList")
    @ResponseBody
    public Object bwgList(@RequestParam Map<String, String> paramMap,HttpServletRequest req) {
        Integer userid=(Integer)req.getSession().getAttribute("id");
        PageInfo<BwgData> pageInfo = backShowService.selectBwgList(paramMap,userid);
        return new TableResponse(0, "查询成功", pageInfo.getList(), pageInfo.getTotal());
    }

    @PostMapping("/save_image")
    @ResponseBody
    public String save_iamge(@RequestParam("file") MultipartFile file,HttpServletRequest req) {
        Integer userid = (Integer) req.getSession().getAttribute("id");
        String type = req.getParameter("type");
        String result="";
        switch (type){
            case "union":
                FrontUnion frontUnion=new FrontUnion();
                frontUnion.setUserid(userid);
                result = FileUtil.uploadfile(file,"image//userlogo");
                frontUnion.setPicid(result);
                frontUnion.setStatus("dsh");
                backShowService.changeFrontUnionDescribe(frontUnion);
                return result;
            case "member":
                Integer bwgid=Integer.parseInt(req.getParameter("bwgid"));
                FrontMember frontMember=new FrontMember();
                frontMember.setBwgid(bwgid);
                result = FileUtil.uploadfile(file,"image//bwglogo");
                frontMember.setPicid(result);
                frontMember.setStatus("dsh");
                backShowService.changeFrontMemberDescribe(frontMember);
                return result;
        }

        return result;

    }

    @PostMapping("/changeDescribe")
    @ResponseBody
    public String changeDesribe(@RequestBody FrontUnion frontUnion, HttpServletRequest req) {
        Integer userid = (Integer) req.getSession().getAttribute("id");
        frontUnion.setUserid(userid);
        frontUnion.setStatus("dsh");
        backContentService.delFrontFcCheckByIdType(frontUnion.getUserid(),"user");
        backShowService.changeFrontUnionDescribe(frontUnion);
        return "success" ;
    }

    @PostMapping("/changeBwgDescribe")
    @ResponseBody
    public String changeBwgDescribe(@RequestBody FrontMember frontMember, HttpServletRequest req) {
        Integer userid = (Integer) req.getSession().getAttribute("id");
        MemBwgWithBLOBs bwg= backShowService.getBwgById(frontMember.getBwgid());
        frontMember.setName(bwg.getName());
        frontMember.setUserid(userid);
        frontMember.setStatus("dsh");
        frontMember.setProvince(bwg.getProvince());
        backContentService.delFrontFcCheckByIdType(frontMember.getUserid(),"museum");
        backShowService.changeFrontMemberDescribe(frontMember);
        return "success" ;
    }

    @PostMapping("/delBwgzt")
    @ResponseBody
    public String delBwgzt(Integer id) {
        try {
            backShowService.delBwgztById(id);
            backContentService.delFrontFcCheckByIdType(id,"museum");
            return "success";
        } catch (Exception e) {
            return "error";
        }

    }

    @PostMapping("/dhyj")
    @ResponseBody
    public String dhyj(@RequestParam Integer id,HttpServletRequest req) {
        String type=req.getParameter("type");
        FrontFcCheck frontFcCheck=backShowService.selectFrontFcCheckById(id,type);
        System.out.println(frontFcCheck.getOpinion());
        return frontFcCheck.getOpinion();
    }

    @RequestMapping("/getLogo")
    public void getUserLogo(HttpServletRequest request,
                            HttpServletResponse response) {
        response.setContentType("image/png"); // 设置返回内容格式
        String picid = request.getParameter("picid");
        System.out.print(picid);
        try {
            File dir = new File("");
            String url = dir.getCanonicalPath() + "\\src\\main\\resources" + picid.replace("/", "\\") ;
            File file = new File(url);// 括号里参数为文件图片路径
            System.out.println(url);
            if (file.exists()) {// 如果文件存在
                InputStream in;
                in = new FileInputStream(file);
                OutputStream os = response.getOutputStream(); // 创建输出流
                byte[] b = new byte[1024];
                while (in.read(b) != -1) {
                    os.write(b);
                }
                in.close();
                os.flush();
                os.close();
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }


}

